<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Search extends MY_Controller {
	function __construct(){
		parent::__construct();
		if($this->uri->segment(4)){show_404();}
	}
	
	public function index(){
		$keyword = $this->input->get('key',TRUE);
		$model='works';
		$keyword = $keyword==''?urldecode($this->uri->segment(2)):$keyword;

		$lastid=(int)$this->input->get('lastid');
		$where=array();
		if(!empty($lastid)){
			$where['w.id < ']=$lastid;
		}
		$sqlkey=mysql_escape_string($keyword);
		$slist=$this->Data_model->db
			->select('w.*,m.mid,m.mname,m.mnick,m.avatar')->from('works w')
			->join('member m','m.mid=w.mid','left')
			->where($where)
			->where('(w.title like \'%'.$sqlkey.'%\' OR w.content like \'%'.$sqlkey.'%\' OR m.mnick like \'%'.$sqlkey.'%\'OR m.mname like \'%'.$sqlkey.'%\' )')
			/*->like('(',NULL)
			->like('title',$keyword)
			->or_like('content',$keyword)
			->or_like('mname',$keyword)
			->or_like('mnick',$keyword)
			->like(')',NULL)*/
			->order_by('w.id DESC')
			->limit(10)
			->get()->result_array();
		//echo $aa=$this->Data_model->db->last_query();
		foreach ($slist as $key => $value) {
			$slist[$key]['title']=csubstr($value['title'],7);
			$slist[$key]['content']=empty($value['content'])?'未填写':csubstr($value['content'],38);
			if($this->member->mid==$value['mid']){
				$slist[$key]['href']=site_url('member/work/'.$value['id']);
				$slist[$key]['spaceurl']=site_url('member');
			}else{
				$slist[$key]['href']=site_url('space/work/'.$value['mid'].'/'.$value['id']);
				$slist[$key]['spaceurl']=site_url('space/'.$value['mid']);
			}
			
			$slist[$key]['puttime']=date('Y.m.d',$value['puttime']);
			if(empty($value['title'])){
				$slist[$key]['title']='未填写';
			}
			if(empty($value['mnick'])){
				$slist[$key]['mnick']=$value['mname'];
			}
		}

		/*$datawhere = array(
				'puttime <'=>time(),
				//'status'=>1,
				//'lang'=>$this->Cache_model->currentLang,
				'title like'=>'%'.$keyword.'%',
				'content like'=>'%'.$keyword.'%',
		);
		$currentpage = intval($this->uri->segment(4));
		$currentpage = $currentpage?$currentpage:1;
		$totalnum = $this->Data_model->getDataNum($datawhere,$model);
		$this->load->library('pagination');
		$pageconfig['base_url'] = site_url('search/'.urlencode($keyword));
		$pageconfig['total_rows'] =$totalnum;
		$pageconfig['per_page'] = 20;
		$pageconfig['uri_segment'] = 4;
		$pageconfig['langurl'] = $this->Cache_model->langurl;
		$this->pagination->initialize($pageconfig);
		$list = $this->Data_model->getData($datawhere,'puttime desc,id desc',$pageconfig['per_page'],($currentpage-1)*$pageconfig['per_page'],$model);*/
		$this->seo($keyword,$keyword,$keyword);
		$res = array(
				'list'=>$slist,
				'isajax'=>$this->input->is_ajax_request(),
				'baselink'=>site_url('search/'.urlencode($keyword)),
				'currentnav'=>array('搜索',htmlspecialchars($keyword)),
				'latest'=>$this->getLatest()
		);
		if($res['isajax']){
			$this->view('search_ajax',$res);
		}else{
			$this->view('search',$res);
		}
	}

	/*
	public function index(){
		$model= $this->input->post('model',TRUE);
		$keyword = $this->input->post('keyword',TRUE);
		$model = $model==''?$this->uri->segment(2):$model;
		$keyword = $keyword==''?urldecode($this->uri->segment(3)):$keyword;
		if($model==''||$this->Data_model->getDataNum(array('varname'=>$model,'issearch'=>1),'model')==0){
			$this->seo(lang('search_error'));
			$actionurl[] = array('name'=>lang('home'),'url'=>base_url($this->Cache_model->langurl));
			$res = array(
					'message'=>lang('search_error'),
					'actionurl'=>$actionurl
			);
			$this->view('message',$res);
		}else{
			$datawhere = array(
					'puttime <'=>time(),
					'status'=>1,
					'lang'=>$this->Cache_model->currentLang,
					'title like'=>'%'.$keyword.'%'
			);
			$currentpage = intval($this->uri->segment(4));
			$currentpage = $currentpage?$currentpage:1;
			$totalnum = $this->Data_model->getDataNum($datawhere,$model);
			$this->load->library('pagination');
			$pageconfig['base_url'] = site_url('search/'.$model.'/'.urlencode($keyword));
			$pageconfig['total_rows'] =$totalnum;
			$pageconfig['per_page'] = 20;
			$pageconfig['uri_segment'] = 4;
			$pageconfig['langurl'] = $this->Cache_model->langurl;
			$this->pagination->initialize($pageconfig);
			$list = $this->Data_model->getData($datawhere,'listorder,puttime desc',$pageconfig['per_page'],($currentpage-1)*$pageconfig['per_page'],$model);
			$this->seo($keyword,$keyword,$keyword);
			$res = array(
					'list'=>$this->Cache_model->handleModelData($list),
					'pagestr'=>$this->pagination->create_links(),
			);
			$this->view('search',$res);
		}
	}*/
}